<?php
declare (strict_types=1);

namespace app\admin\service\content\sensitive;

use app\admin\logic\content\sensitive\Sensitive as SensitiveLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 敏感词管理
 */
class Sensitive extends BaseService
{
    /**
     * @Inject()
     *
     * @var SensitiveLogic
     */
    protected $sensitiveLogic;

    /**
     * 列表
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $where = [];
        Request::has('title', 'get', true) && $where[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $data = $this->sensitiveLogic->getList($where);

        // 定义字段
        $keyList = [
            'title'       => ListItem::make()->title('敏感词名称'),
            'num'         => ListItem::make()->title('触发次数'),
            'status'      => ListItem::make('custom')->title('启用状态'),
            'update_time' => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 添加敏感词
     *
     * @return \eduline\admin\page\interfaces\think\Response|\think\Response
     */
    public function created()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->sensitiveLogic->created($data);

            return $this->parseToData(['count' => $res], 1, '添加成功');
        }

        // 设置表单
        $form = new PageForm;
        // 表单字段
        $form->pageKey = [
            'keyword' => FormItem::make('textarea')->placeholder()->title('关键字')->maxlength(2000)->wordLimit()->rows(4)->required()->help('支持添加多个关键字，一行一个'),
            // 'status'  => FormItem::make('SlideSwitch')->activeValue()->inactiveValue(0)->title('启用状态')
        ];

        $form->saveMethod  = 'POST';
        $form->datas       = ['status' => 1];
        $form->savePostUrl = '/admin/content/sensitive/create';

        return $form->send();
    }

    /**
     * 更新敏感词
     *
     * @param int $id
     * @return \eduline\admin\page\interfaces\think\Response|\think\Response
     */
    public function updated(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->sensitiveLogic->updated($id, $data);

            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = $this->sensitiveLogic->where('id', $id)->findOrFail();

        // 设置表单
        $form = new PageForm;
        // 表单字段
        $form->pageKey = [
            'title' => FormItem::make()->title('名称')->required()->maxlength(200),
            // 'status' => FormItem::make('SlideSwitch')->activeValue()->inactiveValue(0)->title('启用状态')
        ];
        $form->datas   = $data;

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/content/sensitive/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 删除敏感词
     *
     * @param int $id
     * @return \think\Response
     */
    public function deleted(int $id)
    {
        $this->sensitiveLogic->deleted($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 更新字段
     *
     * @param int    $id
     * @param string $field
     * @return \think\Response
     */
    public function updatedBy(int $id, string $field)
    {
        $value = Request::put($field);
        $res   = $this->sensitiveLogic->updatedBy($id, $field, $value);

        return $this->parseToData($res->toArray(), 1, '操作成功');
    }

    /**
     * 批量下架
     *
     * @return \think\Response
     */
    public function disables()
    {
        $ids = Request::put('ids/a');
        $this->sensitiveLogic->batchUpdated($ids, 'status', 0);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量上架
     *
     * @return \think\Response
     */
    public function enables()
    {
        $ids = Request::put('ids/a');
        $this->sensitiveLogic->batchUpdated($ids, 'status', 1);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量删除
     *
     * @param $ids
     * @return \think\Response
     */
    public function deletes($ids)
    {
        $this->sensitiveLogic->deletes($ids);

        return $this->parseToData([], 1, '操作成功');
    }
}
